#!/bin/bash
echo -e  "\e[32m=======开始安装elasticsearch========\e[0m"
if ! docker images | grep "elasticsearch.*7.4.2.*" >/dev/null
then

        echo -e  "\e[33m=======开始拉取elasticsearch镜像========\e[0m"
        docker pull elasticsearch:7.4.2
        echo -e  "\e[33m=======镜像拉取完成========\e[0m"
fi
if docker ps -a | grep "elasticsearch$" &>/dev/null
then
        docker rm -f  elasticsearch &>/dev/null
fi
#配置环境
mkdir -p /mydata/elasticsearch/config
mkdir -p /mydata/elasticsearch/data
echo "http.host: 0.0.0.0" >/mydata/elasticsearch/config/elasticsearch.yml
chmod -R 777 /mydata/elasticsearch/ >/dev/null
#启动容器
docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \
-e  "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms64m -Xmx512m" \
-v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \
-v  /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
-d elasticsearch:7.4.2 >/dev/null
chmod -R 777 /mydata/elasticsearch/ >/dev/null
docker update elasticsearch --restart=always >/dev/null
echo -e "\e[32m======elasticsearch安装完成========\e[0m"